# from mysql.DB_api import db_insert
import asyncio


def dumps(_str):
    # 这里不知道为什么传进来的字符串的引号split不掉
    # 于是手动在头尾添加了一个','和一个'0'
    # 在切割和拼接（感觉号麻烦）
    _list = _str.join(',0')
    k = _list[2: -2].split(',')

    rt = []
    for i in range(len(k)):
        rt.append(int(k[i]))
    return rt


# data:[a,a,a]; battlinglist:[[a,a,a],[a,a,a]...]
def check_win(data, battlinglist):

    z1 = []     # -方向棋子
    z2 = []     # |方向棋子
    z3 = []     # /方向棋子
    z4 = []     # \方向棋子
    flag = False    # 5连标志符

    color = data[2]     # 取出颜色
    x = data[0]     # 取出x
    y = data[1]     # 取出y

    for i in battlinglist:
        xi = i[0]   # 取出循环的x
        yi = i[1]   # 取出循环的y
        colori = i[2]

        # 只判断相同颜色
        if colori != color:
            continue
        if x == xi:
            z1.append(yi)
            flag = check_five(z1)
            if flag:
                print('z1:', z1)
                break
        if y == yi:
            z2.append(xi)
            flag = check_five(z2)
            if flag:
                print('z2:', z2)
                break
        if xi - x == yi - y:
            z3.append(xi)
            flag = check_five(z3)
            if flag:
                print('z3:', z3)
                break
        if xi - x == y - yi:
            z4.append(xi)
            flag = check_five(z4)
            if flag:
                print('z4:', z4)
                break
        else:
            continue

    if flag:
        print('color:', color)
        return color
    else:
        return -1


def check_five(list1):
    list1.sort()
    count_one = []
    rt = False
    mm = 0

    for i in list1:
        if i == list1[0]:
            mm = i
            continue
        count_one.append(i - mm)
        mm = i

    for i in range(len(count_one)-3):
        counter = count_one[i:i + 4]
        if counter.count(1) == 4:
            print('5连')
            rt = True

    return rt

